//
//  TimeLabel.m
//  Sports-Go
//
//  Created by rimi on 16/9/22.
//  Copyright © 2016年 YRL. All rights reserved.
//

#import "TimeLabel.h"

@interface TimeLabel ()
@property (nonatomic, strong)NSTimer *timer;
@end
@implementation TimeLabel

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.textAlignment = NSTextAlignmentCenter;
    
       
    }
    return self;
}
- (void)controlTimeWith:(BOOL)isBegin{
  
    if (isBegin == NO && self.minute > 0) {
       
         self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeHeadle) userInfo:nil repeats:YES];
        
    }else{
        [self.timer invalidate];
        self.timer = nil;
    }

    
}

- (void)timeHeadle{
    
    self.second--;
    
    if (self.second== -1) {
        self.second=59;
        self.minute--;
    }
    
    self.text = [NSString stringWithFormat:@"%ld:%ld",(long)self.minute,(long)self.second];
    if (self.second==0 && self.minute<=0 ) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"playNext" object:nil];
        [self.timer invalidate];
        self.timer = nil;
    }
}
//暂停
- (void)suspendedTimeWith:(BOOL)flag{
    if (flag) {
     [self.timer setFireDate:[NSDate distantFuture]];
    }else{
    [self.timer setFireDate:[NSDate distantPast]];
    }

}
- (void)clearTime{
    self.minute = 0;
    self.second = 0;
    [self.timer invalidate];
    self.timer = nil;
}
@end
